package main.java.common;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;

import main.java.ERMDE.controllers.IProjectController;
import main.java.ERMDE.infrastructure.IterableExtensions;

public class ProjectOpenView extends JPanel{
    
    private static final long serialVersionUID = 1L;
   
    private IProjectController projectController;
    private DefaultListModel<String> listModel = new DefaultListModel<String>();
    private JList<String> projectList = new JList<String>(listModel);
    private JScrollPane projectWrapper = new JScrollPane(projectList);
    private JDialog openDlg = new JDialog();
    private JButton btnLoad;
    private JComboBox<String> typeCombo = new JComboBox<String>();
    private JButton btnValidateProject;
    private JTree tree;
    
    
    public ProjectOpenView(){

	// Open project - Select Type
	DefaultListCellRenderer dlcr = new DefaultListCellRenderer();
	dlcr.setHorizontalAlignment(DefaultListCellRenderer.CENTER);
	this.typeCombo.setRenderer(dlcr);
	this.typeCombo.addItem("ERM");
	this.typeCombo.addItem("FSM");
	this.typeCombo.addItem("DM");
	
	// Open project dialog
	openDlg.add(typeCombo, BorderLayout.NORTH);
	openDlg.add(projectWrapper, BorderLayout.CENTER);
	this.btnLoad = new JButton("Load");
	this.btnValidateProject = new JButton("Validate");
    }
    
    private void updateOpenProjectList() {
	
	String type = typeCombo.getItemAt(typeCombo.getSelectedIndex());
	listModel.removeAllElements();
	
	for (String project : projectController.getExistingProjects(type)) {
	    listModel.addElement(project);
	}
    }
    
    private void initializeTypeCombo(){
	
	this.typeCombo.addActionListener(new ActionListener() {
	    @Override
	    public void actionPerformed(ActionEvent arg0) {
		updateOpenProjectList();
	    }
	});
    }
    
    private void initializeValidateProject(){

	this.btnValidateProject.addActionListener(new ActionListener() {
	    private String[] toleranceOptions = { "Low", "Medium", "High" };
	    @Override
	    public void actionPerformed(ActionEvent e) {
		openDlg.setVisible(false);
		String name = (String) projectList.getSelectedValue();
		if (name != null) {
		    if (((String) typeCombo.getSelectedItem()).equals("ERM")) {
			String tolerance = (String) JOptionPane.showInputDialog(null,
					"Select the tolerance level",
					"Project Validation",
					JOptionPane.QUESTION_MESSAGE, null,
					toleranceOptions, "Low");
			if (tolerance != null) {
			    int toleranceLevel = IterableExtensions.getListOf(
				    toleranceOptions).indexOf(tolerance) + 1;
			    projectController.validateERMProject(name,
				    toleranceLevel);
			}
		    } else {
			projectController.validateFSMProject(name);
		    }
		}
	    }
	});
	
    }

    private void initializeLoad() {
	this.btnLoad.addMouseListener(new MouseAdapter() {
	    @Override
	    public void mouseClicked(MouseEvent e) {
		openDlg.setVisible(false);
		String name = (String) projectList.getSelectedValue();
		if (name != null) {
		    try {
			projectController.openProject(name);
		    } catch (Exception ex) {
			ex.printStackTrace();
		    }
		    tree.setModel(projectController.getProjectTree());
		    tree.setCellRenderer(new DefaultTreeCellRenderer() {
			@Override
			public Component getTreeCellRendererComponent(
				JTree pTree, Object pValue,
				boolean pIsSelected, boolean pIsExpanded,
				boolean pIsLeaf, int pRow, boolean pHasFocus) {
			    DefaultMutableTreeNode node = (DefaultMutableTreeNode) pValue;
			    super.getTreeCellRendererComponent(pTree, pValue,
				    pIsSelected, pIsExpanded, pIsLeaf, pRow,
				    pHasFocus);
			    if (node.isRoot()) {
				setForeground(Color.red);
			    }
			    return (this);
			}
		    });
		}
	    }
	});
    }	
    
    public void openDialog(JButton button){
	updateOpenProjectList();
	openDlg.setLocationRelativeTo(button);
	openDlg.setTitle("Open Project");
	openDlg.setVisible(true);
	openDlg.remove(btnValidateProject);
	openDlg.add(btnLoad, BorderLayout.SOUTH);
	openDlg.pack();
	initializeTypeCombo();
	initializeLoad();
    }
    
    public void validateDialog(JButton button){
	updateOpenProjectList();
	openDlg.setLocationRelativeTo(button);
	openDlg.setTitle("Validate Project");
	openDlg.setVisible(true);
	openDlg.remove(btnLoad);
	openDlg.add(btnValidateProject, BorderLayout.SOUTH);
	openDlg.pack();
	initializeTypeCombo();
	initializeValidateProject();
    }
    

    public void setController(IProjectController projectController) {
	this.projectController = projectController;	
    }
    
    public void setTree(JTree tree){
	this.tree = tree;
    }
    
}
